<?php

class page_parent_lookup extends Model {

	public $lookup_id = false;
public $page_id = false;
public $page_parent_id = false;
public $page_pos = false;



    function page_parent_lookup()
    {
        // Call the Model constructor
        parent::Model();
		$this->_table_name = 'page_parent_lookup';
		$this->_primary_key = 'lookup_id';
    }
	
	public function search_page_parent($iFindPageId = 0, $iTopLevelPageId = 0)
	{
		$aClassVars = get_class_vars(get_class($this));
		
		$whereArray = array();
		
		foreach ($aClassVars as $vVar => $vVal)
		{
			if (in_array($vVar, $this->_ignore_vars) === false)
			{
				if ($this->$vVar !== false)
				{
					$whereArray[$vVar] = $this->$vVar;
				}		
			}
		}
		
		
		$this->db->select();
		$this->db->from($this->_table_name);
		
		if (count($whereArray) > 0)
		{
			$this->db->where($whereArray);
		}
		
		$this->db->limit(1);
		
		$qPage = $this->db->get();
		
		if ($iTopLevelPageId !== 0)
		{
			//echo $this->db->last_query().'<hr />';
		}
		
		//if (($qPage->row(0)->page_id === $qPage->row(0)->page_parent_id) || (($iFindPageId != 0) && ($iFindPageId == $qPage->row(0)->page_id) && ($iRecursive === true)))
		if (($qPage->row(0)->page_id === $qPage->row(0)->page_parent_id) || (($iTopLevelPageId == $qPage->row(0)->page_parent_id)))
		{
			if ($iTopLevelPageId !== 0)
			{
				//echo $qPage->row(0)->page_parent_id.' : '.$qPage->row(0)->page_id.' : '.$iTopLevelPageId.'<hr />';
			}
			return $qPage->row(0)->page_id;
			
		}
		
		$this->page_id = $qPage->row(0)->page_parent_id;
			
		return $this->search_page_parent($iFindPageId, $iTopLevelPageId);
		
	}
}

?>